package my.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;

import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import my.action.article.AddNewArticleAction;
import my.action.article.ArticleAddAction;
import my.action.article.ArticleDeleteAction;
import my.action.article.ArticleDetailAction;
import my.action.article.ArticleDetailIndexAction;
import my.action.article.ArticleEditAction;
import my.action.article.ArticleListAction;
import my.action.article.ArticleSearchAction;
import my.action.article.ArticleUpdateAction;
import my.action.article.PageLoadAction;
import my.action.article.PageNewArticleAction;
import my.action.authentication.AuthenticationDeleteAction;
import my.action.authentication.AuthenticationEditAction;
import my.action.authentication.AuthenticationLogOutAction;
import my.action.authentication.AuthenticationLoginAction;
import my.action.author.AuthorAddAction;
import my.action.author.AuthorDeleteAction;
import my.action.author.AuthorEditAction;
import my.action.author.AuthorListAction;
import my.action.author.AuthorUpdateAction;
import my.action.author.getAuthorAction;
import my.db.DTO.AuthorDTO;
import my.db.DTO.FileUpload;

/**
 * Servlet implementation class ArticleFrontController
 */
@WebServlet("*.article")
public class ArticleFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
   	 String RequestURI=request.getRequestURI();
		 String contextPath=request.getContextPath();
		 String command=RequestURI.substring(contextPath.length()); 
		 System.out.println("command hell:   "+command);		 
		 ActionForward forward = new ActionForward();
		 Action action = null;
		 /*
		  * Action login 
		  */
		 if(command.equals("/login.article")){	
			 action  = new AuthenticationLoginAction();
				try {
					forward=action.execute(request, response);
				} catch (Exception e){
					e.printStackTrace();
				}
		/*
		 * Action add new author
		 */
		 }else if(command.equals("/add_new_author.article")){
			 action  = new AuthorAddAction();
				try {
					forward=action.execute(request, response);
				} catch (Exception e){
					e.printStackTrace();
				}
		/*
		 * Action page new article
		 */
		 }else if(command.equals("/New_Article.article")){
			 action=new PageNewArticleAction();
			 try {
				forward=action.execute(request, response);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		/*
		 * Action add new article
		 */
		 }else if(command.equals("/add_new_Article.article")){
			FileUpload f=new FileUpload();		   
			action=new AddNewArticleAction();
			try{
				forward=action.execute(request, response);
			}catch(Exception e){}
		/*
		 * Action delete user account 
		 */
		 }else if(command.equals("/deleteaccount.article")){
			 action  = new AuthenticationDeleteAction();
				try {
					forward=action.execute(request, response);
				} catch (Exception e){
					e.printStackTrace();
				}
		/*
		 * Action update account user
		 */
		 }else if(command.equals("/update_account.article")){
			 action  = new AuthenticationEditAction();
				try {
					forward=action.execute(request, response);
				} catch (Exception e){
					e.printStackTrace();
				}
		/*
		 * Action load homepage 
		 */
		 }else if(command.equals("/PageLoad.article")){
			 action =new PageLoadAction();
			 try{
				 forward=action.execute(request, response);
				 
			 }catch(Exception e	){
				 e.printStackTrace();
			 }
		/*
		 * Action add article
		 */
		 }else if(command.equals("/addarticle.article")){
			 action  = new ArticleAddAction();
				try {
					forward=action.execute(request, response);
				} catch (Exception e){
					e.printStackTrace();
				}
		/*
		 * Action loading page admin
		 */
		 }else if(command.equals("/AdminPage.article")){
			action=new AuthorListAction();
			try{
				forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action detail author 
		 */
		 }else if(command.equals("/AuthorDetail.article")){
			 action=new getAuthorAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action edit and update article
		 */
		 }else if(command.equals("/edit_article.article")){
			 action=new ArticleEditAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action delete article
		 */
		 }else if(command.equals("/delete_article.article")){
			 action=new ArticleDeleteAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action article detail
		 */
		 }else if(command.equals("/ArticleDetail.article")){
			 action=new ArticleDetailAction();
			 try{
				 forward=action.execute(request, response);
			 }catch(Exception e){}
		/*
		 * Action update article
		 */
		 }else if(command.equals("/update_article.article")){
			 action=new ArticleUpdateAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action log out account
		 */
		 }else if(command.equals("/logout.article")){
			 action=new AuthenticationLogOutAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action get author info 
		 */
		 }else if(command.equals("/list_author.article")){
			 action=new AuthorListAction();
			 try{
					forward=action.execute(request, response);
			 }catch(Exception e){e.printStackTrace();}
		/*
		 * Action get article info
		 */
		 }else if(command.equals("/list_article.article")){
			 action=new ArticleListAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action delete author info
		 */
		 }else if(command.equals("/delete_author.article")){
			 action=new AuthorDeleteAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action get author info for update
		 */
		 }else if(command.equals("/edit_author.article")){
			 action=new AuthorEditAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action update author info
		 */
		 }else if(command.equals("/update_author.article")){
			 action=new AuthorUpdateAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action search all article
		 */
		 }else if(command.equals("/search_article.article")){
			 action=new ArticleSearchAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		/*
		 * Action article detail for homepage
		 */
		 }else if(command.equals("/article_detail_index.article")){
			 action=new ArticleDetailIndexAction();
			 try{
					forward=action.execute(request, response);
			}catch(Exception e){e.printStackTrace();}
		 }
		 if(forward != null){			 	
				if(forward.isRedirect()){ // true
					System.out.println("true");
					response.sendRedirect(forward.getPath());
				}else{                    // false
					System.out.println("false");
					RequestDispatcher dispatcher =	request.getRequestDispatcher(forward.getPath());
					dispatcher.forward(request, response);
				}
			} 
   }
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleFrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

}
